import axios from 'axios';
import { message } from '@/utils';

// 创建axios实例
const service = axios.create({
  baseURL: '/api', // api 的 base_url
  timeout: 30000, // 请求超时时间
  withCredentials: true,
});

// respone拦截器
service.interceptors.response.use(
  (response) => {
    const { data } = response;
    return data;
  },
  (error) => {
    const { response } = error;
    if (response) {
      const { status, data, config } = response;
      switch (status) {
        case 401:
          message('未登录或登录已过期，请重新登录！');
          break;
        case 409:
          message(data.message || '409 资源冲突！');
          break;
        default:
          break;
      }
      /* eslint-disable */
      console.group(
        `${config.method.toUpperCase()} ${config.url} - error and response:`,
      );
      console.log(error);
      console.log(response);
      console.groupEnd();
      /* eslint-disable */
    }
    return Promise.reject(error);
  },
);

export default service;
